package com.sskj.mine.router;

import android.app.Activity;
import android.content.Context;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.sskj.common.base.AppManager;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.lib.util.TipUtil;
import com.sskj.mine.component.DaggerUserDataComponent;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;

@Route(path = "/mine/service/logout")
public class LogoutProviderImpl implements LogoutProvider {
    @Inject
    UserViewModel userViewModel;
    private static MaterialDialog exitTip;
    private static boolean isShow=false;
    @Override
    public void logout() {
        DaggerUserDataComponent.create().inject(this);
        SPUtil.clear();
        HttpHeaders commonHeaders = OkGo.getInstance().getCommonHeaders();
        commonHeaders.clear();
        OkGo.getInstance().addCommonHeaders(commonHeaders);
        userViewModel.clear();

        AppManager.getAppManager().finishAllActivity("com.sskj.jingbi.ui.activity.MainActivity");
        ARouter.getInstance().build(RConfig.MINE_LOGIN).navigation();



    }

    @Override
    public void logout(String msg) {
        if (exitTip != null && exitTip.isShowing()||isShow) {
            return;
        }
        isShow=true;
        Activity activity = AppManager.getAppManager().currentActivity();
        if (activity==null)
            return;
        AndroidSchedulers.mainThread().scheduleDirect(() -> {

            exitTip = TipUtil.getExitTip(activity, msg, () -> {
                exitTip.dismiss();
                logout();
            });
            exitTip.show();
        });
    }

    @Override
    public void init(Context context) {

    }
}
